package com.example.music.Utils;

import android.app.Dialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;
import com.example.music.WebViewActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FullScreenDialogFragment extends DialogFragment {
    SearchView searchView;
    private DialogInterface.OnDismissListener onDismissListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,R.style.MaterialSearch);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(true);
        View rootView = inflater.inflate(R.layout.full_screen_dialog_layout, container, false);
        searchView = rootView.findViewById(R.id.searchView);
        searchView.setQueryHint("Search...");   // 设置提示文本和是否显示查询
        searchView.setIconifiedByDefault(false);//不点击图标就可点开

        //更换搜索栏的文字颜色,大小
        EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
        int textColor = Color.WHITE; // 你可以设置为你想要的颜色
        searchEditText.setTextColor(textColor);
        int hintColor = Color.GRAY; // 你可以设置为你想要的颜色
        searchEditText.setHintTextColor(hintColor);
        searchEditText.setTextSize(22);

        // 设置宽度为屏宽、靠近屏幕底部。
        final Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        //布局设置
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.TOP;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);

        //初始化搜索列表
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("my_prefs",getContext().MODE_PRIVATE);
        Set<String> searchSet = sharedPreferences.getStringSet("searchList", null);
        List<String> searchList = new ArrayList<>(searchSet);

        RecyclerView searchRecyleView = rootView.findViewById(R.id.search_histroy);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        searchRecyleView.setLayoutManager(layoutManager);

        MyAdapter2 adapter = new MyAdapter2(getContext(),searchList);
        searchRecyleView.setAdapter(adapter);

        //设置搜索监听
        if(searchView!=null) {
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    Set<String> set = sharedPreferences.getStringSet("searchList", null);
                    if (set == null) {
                        set = new HashSet<>();
                    } else {
                        set = new HashSet<>(set); // 将不可变的Set转换为可变的Set
                    }
                    set.add(query);
                    editor.putStringSet("searchList", set);
                    editor.commit();
                    set = sharedPreferences.getStringSet("searchList", null);
                    Log.e("set:",set.toString());

                    //更新视图
                    List<String> List = new ArrayList<>(set);

                    Intent intent = new Intent(getActivity(), WebViewActivity.class);
                    intent.putExtra("query", query);
                    startActivity(intent);

                    adapter.updateData(List);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    // 处理搜索文本的变化
                    return true;
                }
            });
        }

        return rootView;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onDismissListener != null) {
            onDismissListener.onDismiss(dialog);
        }
    }
    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        this.onDismissListener = listener;
    }
}
